package com.cy.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * <br>@class: InnerSingleton
 * <br>@description: 私有内部类单机
 * <br>@author: cy
 * <br>@date: 2020-03-28 22:07:43
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
public class InnerSingleton {
    public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println("test");
        for (int i = 0; i <10 ; i++) {
            new Thread(()->{
            //Lambda
                System.out.println(InnerClassSingleton.getInstance());
            }).start();
        }
        Constructor<InnerClassSingleton> declaredConstructor = InnerClassSingleton.class.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        InnerClassSingleton h = declaredConstructor.newInstance();
        System.out.println("sssssssss"+h);
    }
}

class InnerClassSingleton {
    private static class InnerClassHolder {
        //私有内部类模式
        private static InnerClassSingleton obj = new InnerClassSingleton();
    }

    private InnerClassSingleton() {
        if(InnerClassHolder.obj!=null) {
            throw new RuntimeException("反射多例错误");
        }
    }


    public static InnerClassSingleton getInstance() {

        return InnerClassHolder.obj;
    }
}